package com.ruoyi.web.controller.materialsRequisitiones;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.MatePurRequisition;
import com.ruoyi.domain.MaterialsRequisitiones;
import com.ruoyi.domain.SysMaterialPlan;
import com.ruoyi.domain.ThisStock;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IMaterialsRequisitionesService;
import com.ruoyi.service.ISysMaterialPlanService;
import com.ruoyi.service.IThisStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 材料申请单Controller
 * 
 * @author ruoyi
 * @date 2020-07-23
 */
@Controller
@RequestMapping("/ruoyi/requisitionesVerify")
public class MaterialsRequisitionesVerifyController extends BaseController
{
    private String prefix = "ruoyi/requisitiones";

    @Autowired
    private IMaterialsRequisitionesService materialsRequisitionesService;

    @Autowired
    private ISysMaterialPlanService sysMaterialPlanService;

    @Autowired
    private IThisStockService thisStockService;

    @RequiresPermissions("ruoyi:requisitionesVerify:view")
    @GetMapping()
    public String requisitiones()
    {
        return prefix + "/requisitionesVerify";
    }

    /**
     * 查询材料申请单
     */
    @RequiresPermissions("ruoyi:requisitiones:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialsRequisitiones materialsRequisitiones)
    {
        startPage();
        List<MaterialsRequisitiones> list = materialsRequisitionesService.selectMaterialsRequisitionesList(materialsRequisitiones);
        return getDataTable(list);
    }
    /**
     * 查询总项目对应的材料申请单
     */
    @RequiresPermissions("ruoyi:requisitiones:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long mId)
    {
        startPage();
        List<MaterialsRequisitiones> list = materialsRequisitionesService.selectMaterialsRequisitionesLists(mId);
        list.removeIf(m -> m.getmStatus() == 0);
        return getDataTable(list);
    }

    /**
     * 导出材料申请单列表
     */
    @RequiresPermissions("ruoyi:requisitiones:export")
    @Log(title = "材料申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialsRequisitiones materialsRequisitiones)
    {
        List<MaterialsRequisitiones> list = materialsRequisitionesService.selectMaterialsRequisitionesList(materialsRequisitiones);
        ExcelUtil<MaterialsRequisitiones> util = new ExcelUtil<MaterialsRequisitiones>(MaterialsRequisitiones.class);
        return util.exportExcel(list, "requisitiones");
    }

    /**
     * 新增材料申请单
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map)
    {
        map.put("pId", pId);
        List<SysMaterialPlan> sysMaterialPlanList = sysMaterialPlanService.selectMaterialsRequisitionesLists(pId);
        map.put("sysMaterialPlanList",sysMaterialPlanList);
        return prefix + "/add";
    }

    /**
     * 新增保存材料申请单
     */
    @RequiresPermissions("ruoyi:requisitiones:add")
    @Log(title = "材料申请单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialsRequisitiones materialsRequisitiones)
    {
        //自动获取申请人
        materialsRequisitiones.setmPeoples(ShiroUtils.getLoginName());
        return toAjax(materialsRequisitionesService.insertMaterialsRequisitiones(materialsRequisitiones));
    }

    /**
     * 删除材料申请单
     */
    @RequiresPermissions("ruoyi:requisitiones:remove")
    @Log(title = "材料申请单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(materialsRequisitionesService.deleteMaterialsRequisitionesByIds(ids));
    }

    /**
     * 通过申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/consent")
    @ResponseBody
    public AjaxResult consent(Long mMid) {
        materialsRequisitionesService.conExaStatus(mMid);
        MaterialsRequisitiones mr = new MaterialsRequisitiones();
        MaterialsRequisitiones materialsRequisitiones1 = materialsRequisitionesService.selectMaterialsRequisitionesById(mMid);
        System.out.println(materialsRequisitiones1);
        mr.setmMid(mMid);
        mr.setmBuy(0L);
        mr.setmLocal(0L);
        System.out.println(mr.getmBuy());
        mr.setmUnbuy(materialsRequisitiones1.getMatNumbers());
        System.out.println(materialsRequisitiones1.getMatNumbers());
        mr.setmIsover(0L);
        System.out.println(mr.getmIsover());
        materialsRequisitionesService.updateMaterialsRequisitiones(mr);

        //通过id查询对应的材料对象
        MaterialsRequisitiones materialsRequisitiones=materialsRequisitionesService.ExaId(mMid);
        //获取到申请通过的材料名称
        String name =materialsRequisitiones.getMatDetailss();
        //获取到申请通过的材料数量
        Long num = materialsRequisitiones.getMatNumbers();
        //获取到申请通过材料所在的总项目
        Long mid = materialsRequisitiones.getmId();
        //查询本厂库存整个表(每个项目下的)
        List<ThisStock> thisStockList = thisStockService.selectThisStockLists(mid);
        //单独提出name字段成为一个集合
        List<String> list = thisStockList.stream().map(ThisStock::gettName).collect(Collectors.toList());
        //判断是否存在  不存在返回-1
        int i = list.indexOf(name);
        //如果是-1 就代表集合当中没有这个元素
        if (-1 !=i) {
            //如果本厂库存里有这个材料就查询出id
            ThisStock thisStock = thisStockService.selectThisStockByIds(mid, name);
            //数据库原有的材料数量
            Long gettNumnum = thisStock.gettNum();
            //只添加材料的数量 申请数量+原有数量
            Long anum = num + gettNumnum;
            return toAjax(thisStockService.updateThisStocks(anum, name));
        } else {
            ThisStock thisStock = new ThisStock();
            thisStock.settName(name);
            thisStock.settNum(num);
            thisStock.settId(mid);
            return toAjax(thisStockService.insertThisStock(thisStock));
        }
    }
    /**
     * 驳回【审批】
     */
    @GetMapping("/edit/{mMid}")
    public String edit(@PathVariable("mMid") Long mMid, ModelMap mmap)
    {
        materialsRequisitionesService.editExaStatus(mMid);
        MaterialsRequisitiones materialsRequisitiones = materialsRequisitionesService.selectMaterialsRequisitionesById(mMid);
        mmap.put("materialsRequisitiones", materialsRequisitiones);
        return prefix + "/editVerify";
    }

    /**
     * 驳回保存【审批】
     */
    @RequiresPermissions("ruoyi:requisitiones:edit")
    @Log(title = "材料申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialsRequisitiones materialsRequisitiones)
    {
        return toAjax(materialsRequisitionesService.updateMaterialsRequisitiones(materialsRequisitiones));
    }
}
